#!/usr/bin/env bash
#
# Copyright 2016-2018 Axel Huebl, Rene Widera
#
# This file is part of PIConGPU.
#
# PIConGPU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PIConGPU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PIConGPU.
# If not, see <http://www.gnu.org/licenses/>.
#

# search recursive inside a folder if a file contains end-of-line
#   (EOL) white spaces
#
# @result 0 if no files are found, else 1
#
# note: .md files can contain EOL white spaces for syntax reasons
#       (newline without starting a new paragraph)

ok=0
files=()

pattern="\.def$|\.h$|\.cpp$|\.cu$|\.hpp$|\.tpp$|\.kernel$|\.loader$|"\
"\.param$|\.unitless$|\.sh$|\.bash$|\.cfg$|\.tpl$|\.conf$|"\
"\.awk$|\.gnuplot$|\.cmake$|\.profile$|\.example$|\.py$|"\
"cmakeFlags|CMakeLists\.txt|src/tools/bin"

for i in $(find . \
                -not -path "./.git/*" \
                -not -path "./thirdParty/*" \
                -type f | \
           grep -P "${pattern}")
do
  grep -q "[[:blank:]]\+$" $i
  if [ $? -eq 0 ]
  then
    files+=($i)
    echo "$i contains EOL white spaces!"
    ok=1
  fi
done

if [ $ok -ne 0 ]
then
  echo ""
  echo "SUMMARY"
  echo "-------"
  echo "Run the following command(s) on the above files to remove your"
  echo "end-of-line (EOL) white spaces:"
  echo ""
  for i in ${files[@]}
  do
    echo "sed -i 's/[[:blank:]]\+$//' $i"
  done
fi

exit $ok
